import JSEncrypt from 'jsencrypt/bin/jsencrypt'

export interface JSEncryptType {
  /**
   * AES加密
   *
   * @param contentStr 内容字符串
   */
  encrypt(contentStr: string): string
  /**
   * AES解密
   *
   * @param securityStr 加密字符串
   */
  decrypt(securityStr: string): string
}

export class JSEncryptUtils implements JSEncryptType {
  // 密钥对生成 http://web.chacuo.net/netrsakeypair
  publicKey =
    'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwqwTacjQ1dogskeFCyRAIZSAJSBc15z7r1yCRwKFAmw8FD1SA1ONPaoihszZlExEgsQR04fi6VTIKAHXnm88VC0JhZ2I77xtYPuiD+' +
    'WhPKk5heFXdFIAJDMEEIAd0Cq+' +
    'fHZ//xWf0wGCbBXTyXKtvYIMiQIZZxX/tRHEx0O6iAL8YuOfYJOMxg0k9CfdGPhspGb+' +
    'QMEc2DCi9Hc2U3FSLAd8cf8AVdyPqeUio+' +
    'z3aL3hmZ26lRgXzaSyq22+' +
    'yK7vTHA0ozrHTgBqFpTFCIMvbfQJWos10qFp5tCb1Sb4LLGhTyQhnj8K6SuhTEbzUnmdcEWgM3FFuAaKOQUXNKLUGQIDAQAB'

  privateKey = 'unsecurity'

  /**
   * 公钥加密
   *
   * @param contentStr 内容字符串
   */
  encrypt(contentStr: string): string {
    const encryptor = new JSEncrypt()
    // 设置公钥
    encryptor.setPublicKey(this.publicKey)
    // 对数据进行加密
    return encryptor.encrypt(contentStr)
  }

  /**
   * 私钥解密
   *
   * @param securityStr 加密字符串
   */
  decrypt(securityStr: string): string {
    const encryptor = new JSEncrypt()
    // 设置私钥
    encryptor.setPrivateKey(this.privateKey)
    // 对数据进行解密
    return encryptor.decrypt(securityStr)
  }
}

const jsEncryptUtils: JSEncryptUtils = new JSEncryptUtils()

export default jsEncryptUtils
